
package com.dstz.form.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import com.dstz.base.core.util.BeanUtils;

public abstract class DateUtils {
    
    
    /**
     * yyyy-MM-dd HH:mm:ss 日期格式
     **/
    static String DATE_FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
    
    /**
     * yyyy-MM-dd 日期格式
     **/
    static String DATE_FORMAT_DATE = "yyyy-MM-dd";
    
    /**
     * yyyy-MM-dd HH:mm 时间格式
     **/
    static String DATE_FORMAT_DATETIME_NOSECOND = "yyyy-MM-dd HH:mm";
    
    /**
     * HH:mm:ss 时间格式
     **/
    static String DATE_FORMAT_TIME = "HH:mm:ss";
    
    /**
     * HH:mm 时间格式
     **/
    static String DATE_FORMAT_TIME_NOSECOND = "HH:mm";
    
    /**
     * yyyy-MM-dd HH:mm:ss.SSS 时间格式
     **/
    static String DATE_FORMAT_TIMESTAMP = "yyyy-MM-dd HH:mm:ss.SSS";
    
    public static final String ISO_DATE_FORMAT = "yyyyMMdd";
    
    public static final String ISO_EXPANDED_DATE_FORMAT = "yyyy-MM-dd";
    
    public static final String ISO_TIME_FORMAT = "HHmmssSSSzzz";
    
    public static final String ISO_EXPANDED_TIME_FORMAT = "HH:mm:ss,SSSzzz";
    
    public static final String ISO_DATE_TIME_FORMAT = "yyyyMMdd'T'HHmmssSSSzzz";
    
    public static final String ISO_EXPANDED_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss,SSSzzz";
    
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
    
    public static final String DEFAULT_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    
    public static final long ONE_SECOND = 1000L;
    
    public static final long ONE_MINUTE = 60000L;
    
    public static final long ONE_HOUR = 3600000L;
    
    public static final long ONE_DAY = 86400000L;
    
    public static final long ONE_WEEK = 604800000L;
    
    public static final long ONE_YEAR = 31536000000L;
    
    /** 一个很久很久以前的时间(格林威治的起始时间. 1970-01-01 00:00:00) */
    public static final Date LONG_BEFORE_TIME = string2Date( "1970-01-01 00:00:00" , DEFAULT_DATE_TIME_PATTERN );
    
    /** 一个很久很久以后的时间(该框架可能被遗弃的时间. 2048-01-01 00:00:00) */
    public static final Date LONG_AFTER_TIME = string2Date( "2048-01-01 00:00:00" , DEFAULT_DATE_TIME_PATTERN );
    
    public final static int SECOND = 0;
    
    public final static int MINUTE = 1;
    
    public final static int HOUR = 2;
    
    public final static int DAY = 3;
    
    public final static int MONTH = 4;
    
    /**
     * yyyy-MM-dd 时间格式
     **/
    public static final DateFormat DATE_FORMAT = new SimpleDateFormat( DATE_FORMAT_DATE );
    
    /**
     * yyyy-MM-dd HH:mm:ss 时间格式
     **/
    public static final DateFormat DATETIME_FORMAT = new SimpleDateFormat( DATE_FORMAT_DATETIME );
    
    /**
     * yyyy-MM-dd HH:mm 时间格式
     **/
    public static final DateFormat DATETIME_NOSECOND_FORMAT = new SimpleDateFormat( DATE_FORMAT_DATETIME_NOSECOND );
    
    /**
     * HH:mm:ss 时间格式
     **/
    public static final DateFormat TIME_FORMAT = new SimpleDateFormat( DATE_FORMAT_TIME );
    
    /**
     * HH:mm 时间格式
     **/
    public static final DateFormat TIME_NOSECOND_FORMAT = new SimpleDateFormat( DATE_FORMAT_TIME_NOSECOND );
    
    /**
     * yyyy-MM-dd HH:mm:ss.SSS 时间格式
     **/
    public static final DateFormat TIMESTAMP_FORMAT = new SimpleDateFormat( DATE_FORMAT_TIMESTAMP );
    
    public static Date afterDays ( Date date , long days ) {
        return new Date( date.getTime( ) + 86400000L * days );
    }
    
    public static Date afterDays ( long days ) {
        return new Date( System.currentTimeMillis( ) + 86400000L * days );
    }
    
    public static Date afterDays ( long time , long days ) {
        return new Date( time + 86400000L * days );
    }
    
    public static Date afterDays ( String date , long days ) {
        Date dateTmp = parseDate( date );
        return afterDays( dateTmp , days );
    }
    
    public static Date afterHours ( long hours ) {
        return new Date( System.currentTimeMillis( ) + 3600000L * hours );
    }
    
    public static Date afterHours ( long time , long hours ) {
        return new Date( time + 3600000L * hours );
    }
    
    public static Date afterMilliseconds ( long time , long milliseconds ) {
        return new Date( time + milliseconds );
    }
    
    public static Date afterMinutes ( long minutes ) {
        return new Date( System.currentTimeMillis( ) + 60000L * minutes );
    }
    
    public static Date afterMinutes ( long time , long minutes ) {
        return new Date( time + 60000L * minutes );
    }
    
    public static Date afterSeconds ( long seconds ) {
        return new Date( System.currentTimeMillis( ) + 1000L * seconds );
    }
    
    public static Date afterSeconds ( long time , long seconds ) {
        return new Date( time + 1000L * seconds );
    }
    
    public static Date afterYears ( long years ) {
        return new Date( System.currentTimeMillis( ) + 31536000000L * years );
    }
    
    public static Date afterYears ( long time , long years ) {
        return new Date( time + 31536000000L * years );
    }
    
    /**
     * 将字符串转日期类型，格式yyyy-MM-dd
     * 
     * @param value
     * @return
     **/
    public static Date convertDateString ( String value ) {
        return convertString( value , "yyyy-MM-dd" );
    }
    
    /*
     * public static Date contact(Date date, String time) { if
     * (StringUtils.isEmpty(time)) return date; time = time.replace(":", "");
     * String hour = null; String minute = null; String second = null; int len =
     * time.length(); if (len >= 2) { hour = time.substring(0, 2); } if (len >=
     * 4) { minute = time.substring(2, 4); } if (len >= 6) { second =
     * time.substring(4, 6); }
     * Calendar calendar = new GregorianCalendar(); calendar.setTime(date);
     * calendar.set(11, TypeConverter.toInt(hour, 0)); calendar.set(12,
     * TypeConverter.toInt(minute, 0)); calendar.set(13,
     * TypeConverter.toInt(second, 0)); return calendar.getTime(); }
     **/
    
    /**
     * 将字符串转日期类型，格式yyyy-MM-dd HH:mm:ss
     * 
     * @param value
     * @return
     **/
    public static Date convertString ( String value ) {
        return convertString( value , "yyyy-MM-dd HH:mm:ss" );
    }
    
    /**
     * 将字符串转固定格式的日期类型
     * 
     * @param value
     * @param format
     * @return
     **/
    public static Date convertString ( String value , String format ) {
        SimpleDateFormat sdf = new SimpleDateFormat( format );
        if ( value == null )
            return null;
        try {
            return sdf.parse( value );
        } catch ( Exception e ) {
            return null;
        }
    }
    
    /*
     * public static int getDayOfWeek(String date) { if
     * (StringUtils.isEmpty(date)) return -1; String input = date.replace("-",
     * ""); int len = input.length(); if ((len != 4) && (len != 8)) return -1;
     * int year = 0; int month = 0; int day = 0; if (len == 8) { year =
     * TypeConverter.toInt(input.substring(0, 4), 0); input =
     * input.substring(4); } month = TypeConverter.toInt(input.substring(0, 2),
     * 1); day = TypeConverter.toInt(input.substring(2, 4), 0); try { Calendar
     * calendar = new GregorianCalendar(); calendar.setTime(new Date()); if
     * (year > 0) calendar.set(1, year); if (month > 0) calendar.set(2, month -
     * 1); if (day > 0) calendar.set(5, day); int week = calendar.get(7); return
     * week; } catch (Exception localException) { } return -1; }
     **/
    
    public static String dateString ( String d ) {
        if ( (org.springframework.util.StringUtils.isEmpty( d )) || (d.length( ) != 8) )
            return d;
        char [ ] array = d.toCharArray( );
        char [ ] buff = new char [ 10 ];
        System.arraycopy( array , 0 , buff , 0 , 4 );
        System.arraycopy( array , 4 , buff , 5 , 2 );
        System.arraycopy( array , 6 , buff , 8 , 2 );
        buff [ 7] = 45;
        buff [ 4] = 45;
        return new String( buff );
    }
    
    /**
     * 按格式(yyyy-MM-dd HH:mm:ss )输出date到string
     * 
     * @param date
     * @return
     **/
    public static String formaDatetTime ( Date date ) {
        return DATETIME_FORMAT.format( date );
    }
    
    /**
     * 按格式输出date到string,按照日期类型自动判断
     * 
     * @param date
     *            按格式输出
     * @return 时间字符串
     **/
    public static String format ( Date date ) {
        if ( date instanceof java.sql.Timestamp ) {
            return TIMESTAMP_FORMAT.format( date );
        } else if ( date instanceof java.sql.Time ) {
            return TIME_FORMAT.format( date );
        } else if ( date instanceof java.sql.Date ) {
            return DATE_FORMAT.format( date );
        }
        return DATETIME_FORMAT.format( date );
    }
    
    /**
     * 格式化输出date到string
     * 
     * @param date
     * @param style
     *            格式化参数
     * @return
     **/
    public static String format ( Date date , String style ) {
        DateFormat dateFormat = new SimpleDateFormat( style );
        return dateFormat.format( date );
    }
    
    public static String formatDate ( Date date ) {
        return formatDate( date , "yyyy-MM-dd HH:mm:ss" );
    }
    
    public static String formatDate ( Date date , DateFormat dateFormat ) {
        if ( date == null ) {
            throw new IllegalArgumentException( "date can not be null" );
        }
        if ( dateFormat == null ) {
            throw new IllegalArgumentException( "dateFormat can not be null" );
        }
        return dateFormat.format( date );
    }
    
    public static String formatDate ( Date date , String dateFormatPattern ) {
        return formatDate( date , null , dateFormatPattern );
    }
    
    public static String formatDate ( Date date , TimeZone zone ) {
        return formatDate( date , zone , "yyyy-MM-dd HH:mm:ss" );
    }
    
    public static String formatDate ( Date date , TimeZone zone , String dateFormatPattern ) {
        DateFormat dateFormat = null;
        try {
            dateFormat = new SimpleDateFormat( dateFormatPattern );
        } catch ( Exception ignore ) {
            dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        }
        if ( zone != null )
            dateFormat.setTimeZone( zone );
        return formatDate( date , dateFormat );
    }
    
    /**
     * 将长整型的日期转换成为yyyy-MM-dd格式的日期。
     * 
     * @param mills
     * @return
     * @throws Exception
     **/
    public static String formatDate ( long mills ) throws Exception {
        try {
            if ( mills == 0 )
                return "-";
            Date date = null;
            Calendar ca = Calendar.getInstance( );
            ca.setTimeInMillis( mills );
            date = ca.getTime( );
            SimpleDateFormat myformat;
            
            myformat = new SimpleDateFormat( "yyyy-MM-dd" );
            
            return myformat.format( date );
        } catch ( Exception e ) {
            return "-";
        }
    }
    
    /**
     * 将长整型的日期转换成为yyyy-MM-dd HH:mm:ss格式的日期。
     * 
     * @param mills
     * @return
     * @throws Exception
     **/
    public static String formatTime ( long mills ) throws Exception {
        try {
            if ( mills == 0 )
                return "-";
            Date date = null;
            Calendar ca = Calendar.getInstance( );
            ca.setTimeInMillis( mills );
            date = ca.getTime( );
            SimpleDateFormat myformat;
            
            myformat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
            
            return myformat.format( date );
        } catch ( Exception e ) {
            return "-";
        }
    }
    
    /**
     * 按格式(yyyy-MM-dd HH:mm )输出date到string
     * 
     * @param date
     * @return
     **/
    public static String formatTimeNoSecond ( Date date ) {
        return DATETIME_NOSECOND_FORMAT.format( date );
    }
    
    /**
     * 得到当前日期，例如"2002-11-06"
     * 
     * @return
     **/
    public static String getCurrentDate ( ) {
        SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd" );
        java.util.Date date = new java.util.Date( );
        return formatter.format( date );
    }
    
    public static String getCurrentDate ( String format ) {
        SimpleDateFormat formatter = new SimpleDateFormat( format );
        java.util.Date date = new java.util.Date( );
        return formatter.format( date );
    }
    
    /**
     * 得到当前时间，例如"2002-11-06 17:08:59"
     * 
     * @return
     **/
    public static String getCurrentTime ( ) {
        SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        java.util.Date date = new java.util.Date( );
        return formatter.format( date );
    }
    
    /**
     * 得到当前的毫秒时间
     * 
     * @return
     **/
    public static long getCurrentTimeMillis ( ) {
        Calendar c = Calendar.getInstance( );
        return c.getTimeInMillis( );
    }
    
    /**
     * 取得日期。
     * 
     * @param year
     * @param month
     * @param day
     * @return
     **/
    public static Date getDate ( int year , int month , int date ) {
        return getDate( year , month , date , 0 , 0 , 0 );
    }
    
    /**
     * 取得日期。
     * 
     * @param year
     * @param month
     * @param date
     * @param hourOfDay
     * @param minute
     * @param second
     * @return
     **/
    public static Date getDate ( int year , int month , int date , int hourOfDay , int minute , int second ) {
        Calendar cal = Calendar.getInstance( );
        cal.set( year , month - 1 , date , hourOfDay , minute , second );
        return cal.getTime( );
    }
    
    public static String getDate ( long time , String format ) {
        java.text.SimpleDateFormat formater = new SimpleDateFormat( format );
        return formater.format( new Date( time ) );
    }
    
    /**
     * 根据日期字符串返回date类型。
     * 
     * @param timeString
     * @return
     **/
    public static Date getDateByDateString ( String timeString ) {
        Date date = new Date( );
        try {
            date = parse( timeString , "yy-MM-dd" );
        } catch ( ParseException e ) {
            e.printStackTrace( );
        }
        return date;
    }
    
    /**
     * 取得日期
     * 
     * @param date
     * @return
     **/
    public static String getDateString ( Date date ) {
        if ( date != null ) {
            return new SimpleDateFormat( "yyyy-MM-dd" ).format( date );
        }
        return "";
    }
    
    /**
     * 根据长整型毫秒数返回日期形式:如:2007-01-23
     * 
     * @param millseconds
     * @return
     **/
    public static String getDateString ( long millseconds ) {
        return new SimpleDateFormat( "yyyy-MM-dd" ).format( new java.util.Date( millseconds ) );
    }
    
    /**
     * 根据指定格式取得日期字符格式。
     * 
     * @param formater
     * @return
     **/
    public static String getDateString ( String formater ) {
        return new SimpleDateFormat( formater ).format( new java.util.Date( ) );
    }
    
    /**
     * 根据时间字符串返回date类型
     * 
     * @return
     * @throws ParseException
     **/
    public static Date getDateTimeByTimeString ( String timeString ) {
        Date date = new Date( );
        try {
            date = parse( timeString , "yy-MM-dd HH:mm:ss" );
        } catch ( ParseException e ) {
            e.printStackTrace( );
        }
        return date;
    }
    
    /**
     * 取得时间
     * 
     * @param date
     * @return
     **/
    public static String getDateTimeString ( Date date ) {
        return new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format( date );
    }
    
    /**
     * 取得时间
     * 
     * @param date
     * @param format
     * @return
     **/
    public static String getDateTimeString ( Date date , String format ) {
        return new SimpleDateFormat( format ).format( date );
    }
    
    /**
     * 根据长整型毫秒数返回时间形式:如:2007-01-23 13:45:21
     * 
     * @param millseconds
     * @return
     **/
    public static String getDateTimeString ( long millseconds ) {
        
        return getDate( millseconds , "yyyy-MM-dd HH:mm:ss" );
    }
    
    /**
     * 取得yyyyMMdd格式的时间
     * 
     * @param time
     * @return
     **/
    public static String getDayDate ( long time ) {
        return getDate( time , "yyyyMMdd" );
    }
    
    public static int getDayOfWeek ( Date date ) {
        Calendar calendar = new GregorianCalendar( );
        calendar.setTime( date );
        return calendar.get( 7 );
    }
    
    public static long getDaySeparate ( Date date1 , Date date2 ) {
        return (date2.getTime( ) - date1.getTime( )) / (24 * 60 * 60 * 1000);
    }
    
    /**
     * 取得一个月的天数
     * 
     * @param year
     *            实际年份
     * @param mon
     *            实际月份 1到12月
     * @return
     **/
    public static int getDaysOfMonth ( int year , int mon ) {
        Calendar cal = Calendar.getInstance( );
        cal.set( Calendar.YEAR , year );
        cal.set( Calendar.MONTH , mon - 1 );
        return cal.getActualMaximum( Calendar.DAY_OF_MONTH );
        
    }
    
    /**
     * 获取持续时间
     * 
     * @param time
     * @return
     **/
    public static String getDurationTime ( Date time ) {
        if ( BeanUtils.isEmpty( time ) )
            return "";
        Long millseconds = getTime( time , new Date( ) );
        return getTime( millseconds );
    }
    
    /**
     * 根据长整型毫秒数返回时间形式:
     * 
     * @param millseconds
     * @return
     **/
    public static String getFormatString ( long millseconds , String format ) {
        if ( format == null || format.trim( ).length( ) == 0 ) {
            format = "yyyy-MM-dd";
        }
        format = format.trim( );
        return new SimpleDateFormat( format ).format( new java.util.Date( millseconds ) );
    }
    
    /**
     * 根据给出的日期字符串返回该日期的长整型毫秒数(字符串前后空格不计) 如果strDate的格式不正确将返回0
     * 
     * @param strDate
     *            形如:2007-01-23的日期字符串
     * @return
     **/
    public static long getMillsByDateString ( String strDate ) {
        Calendar cal = Calendar.getInstance( );
        if ( strDate != null && strDate.trim( ).length( ) > 9 ) {
            strDate = strDate.trim( );
            try {
                int year = Integer.parseInt( strDate.substring( 0 , 4 ) );
                int month = Integer.parseInt( strDate.substring( 5 , 7 ) ) - 1;
                int date = Integer.parseInt( strDate.substring( 8 , 10 ) );
                cal.set( year , month , date , 0 , 0 , 0 );
                String str = String.valueOf( cal.getTimeInMillis( ) );
                return Long.parseLong( (str.substring( 0 , str.length( ) - 3 ) + "000") );
                
            } catch ( Exception e ) {
                
            }
        }
        
        return 0;
    }
    
    /**
     * 根据给出的时间字符串返回该时间的长整型毫秒数(字符串前后空格不计) 如果strDateTime的格式不正确将返回0
     * 
     * @param strDateTime
     *            形如:2007-01-23 13:45:21的时间字符串
     * @return
     **/
    public static long getMillsByDateTimeString ( String strDateTime ) {
        Calendar cal = Calendar.getInstance( );
        if ( strDateTime != null && strDateTime.trim( ).length( ) > 18 ) {
            strDateTime = strDateTime.trim( );
            try {
                int year = Integer.parseInt( strDateTime.substring( 0 , 4 ) );
                int month = Integer.parseInt( strDateTime.substring( 5 , 7 ) ) - 1;
                int date = Integer.parseInt( strDateTime.substring( 8 , 10 ) );
                int hour = Integer.parseInt( strDateTime.substring( 11 , 13 ) );
                int minute = Integer.parseInt( strDateTime.substring( 14 , 16 ) );
                int second = Integer.parseInt( strDateTime.substring( 17 , 19 ) );
                cal.set( year , month , date , hour , minute , second );
                return cal.getTimeInMillis( );
            } catch ( Exception e ) {
                
            }
        }
        
        return 0;
    }
    
    /**
     * 根据时间格式如 yyyy-MM-dd HH:mm:ss 的时间取得 UTC 时间
     * 
     * @param 格式如
     *            的字符串yyyy-MM-dd HH:mm:ss 或者 yyyy-mm-dd
     * @return long Utc Millisecond 时间
     **/
    public static long getMillsByTime ( String strTime ) throws Exception {
        try {
            int year , month , day , hour , minute , second;
            if ( strTime.length( ) != 19 && strTime.length( ) != 10 ) {
                throw new Exception( "the time string is wrong." );
            }
            
            year = Integer.parseInt( strTime.substring( 0 , 4 ) );
            month = Integer.parseInt( strTime.substring( 5 , 7 ) ) - 1;
            day = Integer.parseInt( strTime.substring( 8 , 10 ) );
            
            if ( year < 1000 || year > 3000 ) {
                throw new Exception( "the year is wrong." );
            }
            
            if ( month < 0 || month > 12 ) {
                throw new Exception( "the month is wrong." );
            }
            
            if ( day < 1 || day > 31 ) {
                throw new Exception( "the day is wrong" );
            }
            
            Calendar ca = Calendar.getInstance( );
            if ( strTime.length( ) == 19 ) {
                hour = Integer.parseInt( strTime.substring( 11 , 13 ) );
                minute = Integer.parseInt( strTime.substring( 14 , 16 ) );
                second = Integer.parseInt( strTime.substring( 17 , 19 ) );
                
                if ( hour < 0 || hour > 24 ) {
                    throw new Exception( "the hour is wrong." );
                }
                
                if ( minute < 0 || minute > 60 ) {
                    throw new Exception( "the minute is wrong." );
                }
                
                if ( second < 0 || second > 60 ) {
                    throw new Exception( "the second is wrong." );
                }
                
                ca.set( year , month , day , hour , minute , second );
            } else if ( strTime.length( ) == 10 ) {
                ca.set( year , month , day , 0 , 0 , 0 );
            }
            return ca.getTimeInMillis( );
        } catch ( Exception e ) {
            e.printStackTrace( );
            return -1;
        }
    }
    
    /**
     * 取得当前日期的毫秒数
     * 
     * @return
     **/
    public static long getMillsByToday ( ) {
        String str = getDateString( "yyyy-MM-dd" );
        str = String.valueOf( getMillsByDateString( str ) );
        return Long.parseLong( (str.substring( 0 , str.length( ) - 3 ) + "000") );
    }
    
    /**
     * 取得下一天。
     * 
     * @param date
     * @param days
     * @return
     **/
    public static Date getNextDays ( Date date , int days ) {
        Calendar cal = Calendar.getInstance( );
        cal.setTime( date );
        cal.add( Calendar.DATE , days );
        return cal.getTime( );
    }
    
    /**
     * 取得多少天后的数据。
     * 
     * @param days
     * @return
     **/
    public static long getNextDays ( int days ) {
        Calendar cal = Calendar.getInstance( );
        cal.add( Calendar.DATE , days );
        String str = String.valueOf( cal.getTimeInMillis( ) );
        return Long.parseLong( (str.substring( 0 , str.length( ) - 3 ) + "000") );
    }
    
    /**
     * 根据时间单位，时间间隔，当前时间计算出指定时间加上时间间隔后的时间
     * 
     * @param TimeUnit
     *            请使用DateUtils的静态常量
     * @param interval
     *            可为负值
     * @param timeMill
     * @return
     **/
    public static long getNextTime ( int timeUnit , int interval , long timeMill ) {
        Calendar ca = Calendar.getInstance( );
        ca.setTimeInMillis( timeMill );
        switch ( timeUnit ) {
            case DateUtils.SECOND :
                ca.add( Calendar.SECOND , interval );
            break;
            case DateUtils.MINUTE :
                ca.add( Calendar.MINUTE , interval );
            break;
            case DateUtils.HOUR :
                ca.add( Calendar.HOUR , interval );
            break;
            case DateUtils.DAY :
                ca.add( Calendar.DATE , interval );
            break;
            case DateUtils.MONTH :
                ca.add( Calendar.MONTH , interval );
            break;
            default :
                return 0;
        }
        return ca.getTimeInMillis( );
    }
    
    /**
     * 取当前系统日期，并按指定格式或者是默认格式返回
     * 
     * @param style
     * @return
     **/
    public static String getNowByString ( String style ) {
        if ( null == style || "".equals( style ) ) {
            style = "yyyy-MM-dd HH:mm:ss";
        }
        return format( new Date( ) , style );
    }
    
    /**
     * 取得结束时间和开始时间的秒数。
     * 
     * @param endTime
     *            结束时间
     * @param startTime
     *            开始时间
     * @return
     **/
    public static int getSecondDiff ( Date endTime , Date startTime ) {
        long start = startTime.getTime( );
        long end = endTime.getTime( );
        return ( int ) ((end - start) / 1000);
    }
    
    public static long getTime ( Date startTime , Date endTime ) {
        return endTime.getTime( ) - startTime.getTime( );
    }
    
    /**
     * 根据长整形的毫秒数返回字符串类型的时间段
     * 
     * @param millseconds
     * @return
     **/
    public static String getTime ( Long millseconds ) {
        String time = "";
        if ( millseconds == null ) {
            return "";
        }
        int days = ( int ) ( long ) millseconds / 1000 / 60 / 60 / 24;
        if ( days > 0 ) {
            time += days + "天";
        }
        long hourMillseconds = millseconds - days * 1000 * 60 * 60 * 24;
        int hours = ( int ) hourMillseconds / 1000 / 60 / 60;
        if ( hours > 0 ) {
            time += hours + "小时";
        }
        long minuteMillseconds = millseconds - days * 1000 * 60 * 60 * 24 - hours * 1000 * 60 * 60;
        int minutes = ( int ) minuteMillseconds / 1000 / 60;
        if ( minutes > 0 ) {
            time += minutes + "分钟";
        }
        return time;
    }
    
    /**
     * 根据millsecond时间取得 如 yyyy-MM-dd HH:mm:ss 的时间,如果时分秒都为0就返回 yyyy-MM-dd
     * 
     * @param mills
     * @return
     * @throws java.lang.Exception
     **/
    public static String getTimeByMills ( long mills ) throws Exception {
        try {
            if ( mills == 0 )
                return "-";
            Date date = null;
            Calendar ca = Calendar.getInstance( );
            ca.setTimeInMillis( mills );
            date = ca.getTime( );
            SimpleDateFormat myformat;
            
            if ( ca.get( Calendar.HOUR_OF_DAY ) == 0 && ca.get( Calendar.MINUTE ) == 0 && ca.get( Calendar.SECOND ) == 0 ) {
                myformat = new SimpleDateFormat( "yyyy-MM-dd" );
            } else {
                myformat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
            }
            return myformat.format( date );
        } catch ( Exception e ) {
            return "-";
        }
    }
    
    /**
     * 比较两个时间取得两个时间差
     * 
     * @param startTime
     * @param endTime
     * @return
     **/
    public static String getTimeDiff ( String startTime , String endTime ) {
        try {
            String tmp = "";
            SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
            ParsePosition pos = new ParsePosition( 0 );
            ParsePosition pos1 = new ParsePosition( 0 );
            Date dt1 = formatter.parse( startTime , pos );
            Date dt2 = formatter.parse( endTime , pos1 );
            long lDiff = dt2.getTime( ) - dt1.getTime( );
            int days = ( int ) (lDiff / (1000 * 60 * 60 * 24));
            if ( days > 0 ) {
                lDiff = lDiff - days * 1000 * 60 * 60 * 24;
                tmp += days + "天";
            }
            int hours = ( int ) (lDiff / (1000 * 60 * 60));
            if ( hours > 0 )
                lDiff = lDiff - hours * 1000 * 60 * 60;
            tmp += hours + "小时";
            int minute = ( int ) (lDiff / (1000 * 60));
            tmp += minute + "分钟";
            return tmp;
        } catch ( Exception e ) {
            e.printStackTrace( );
            return "-1";
        }
    }
    
    /**
     * 取得某月第一天为星期几。<br>
     * 星期天为1。 星期六为7。
     * 
     * @param year
     * @param mon
     * @return
     **/
    public static int getWeekDayOfMonth ( int year , int mon ) {
        Calendar cal = Calendar.getInstance( );
        cal.set( year , mon - 1 , 1 );
        return cal.get( Calendar.DAY_OF_WEEK );
    }
    
    /**
     * 比较两个时间大小 结束时间是否大于开始时间
     * 
     * @param startTime
     * @param endTime
     * @return
     **/
    public static boolean isTimeLarge ( String startTime , String endTime ) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
            ParsePosition pos = new ParsePosition( 0 );
            ParsePosition pos1 = new ParsePosition( 0 );
            Date dt1 = formatter.parse( startTime , pos );
            Date dt2 = formatter.parse( endTime , pos1 );
            long lDiff = dt2.getTime( ) - dt1.getTime( );
            return lDiff > 0;
        } catch ( Exception e ) {
            
            return false;
        }
    }
    
    public static Timestamp [ ] oneDay ( Date date ) {
        Calendar calendar = new GregorianCalendar( );
        calendar.setTime( date != null ? date : new Date( ) );
        calendar.set( 11 , 0 );
        calendar.set( 12 , 0 );
        calendar.set( 13 , 0 );
        calendar.set( 14 , 0 );
        
        Timestamp [ ] array = new Timestamp [ 2 ];
        array [ 0] = new Timestamp( calendar.getTimeInMillis( ) );
        
        calendar.add( 5 , 1 );
        calendar.add( 14 , -1 );
        array [ 1] = new Timestamp( calendar.getTimeInMillis( ) );
        
        return array;
    }
    
    public static Timestamp [ ] oneDay ( String date ) {
        Date d = null;
        if ( (date != null) && (date.length( ) == 8) ) {
            d = parseDate( date , new String [ ] { "yyyyMMdd" } );
        }
        return oneDay( d );
    }
    
    public static Date onlyDate ( Date time ) {
        Calendar calendar = new GregorianCalendar( );
        calendar.setTime( time );
        calendar.set( 11 , 0 );
        calendar.set( 12 , 0 );
        calendar.set( 13 , 0 );
        calendar.set( 14 , 0 );
        return calendar.getTime( );
    }
    
    /**
     * 根据日期字符串是否含有时间决定转换为日期还是日期时间还是时间
     * 
     * @param dateString
     *            时间字符串
     * @return 格式化的时间
     * @throws ParseException
     **/
    public static Date parse ( String dateString ) {
        try {
            if ( dateString.trim( ).indexOf( " " ) > 0 && dateString.trim( ).indexOf( "." ) > 0 ) {
                return new java.sql.Timestamp( TIMESTAMP_FORMAT.parse( dateString ).getTime( ) );
            } else if ( dateString.trim( ).indexOf( " " ) > 0 ) {
                // 如果有两个:，则有时分秒,一个冒号只有时分
                if ( dateString.trim( ).indexOf( ":" ) != dateString.trim( ).lastIndexOf( ":" ) ) {
                    return new java.sql.Timestamp( DATETIME_FORMAT.parse( dateString ).getTime( ) );
                } else {
                    return new java.sql.Timestamp( DATETIME_NOSECOND_FORMAT.parse( dateString ).getTime( ) );
                }
            } else if ( dateString.indexOf( ":" ) > 0 ) {
                // 如果有两个:，则有时分秒,一个冒号只有时分
                if ( dateString.trim( ).indexOf( ":" ) != dateString.trim( ).lastIndexOf( ":" ) ) {
                    return new java.sql.Time( TIME_FORMAT.parse( dateString ).getTime( ) );
                } else {
                    return new java.sql.Time( TIME_NOSECOND_FORMAT.parse( dateString ).getTime( ) );
                }
            }
            return new java.sql.Date( DATE_FORMAT.parse( dateString ).getTime( ) );
        } catch ( Exception e ) {
            return null;
        }
    }
    
    public static boolean isDate( String dateString ) {
        
        return parse( dateString )!=null;
    }
    
    /**
     * 按指定的格式输出string到date
     * 
     * @param dateString
     *            时间字符串
     * @param style
     *            格式化参数
     * @return 格式化的时间
     * @throws ParseException
     **/
    public static Date parse ( String dateString , String style ) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat( style );
        return dateFormat.parse( dateString );
    }
    
    public static Date parseDate ( String text ) {
        return parseDate( text , new String [ ] { "yyyy-MM-dd HH:mm:ss" , "yyyy-MM-dd" } );
    }
    
    public static Date parseDate ( String text , String [ ] dateFormatPatterns ) {
        return parseDate( text , null , dateFormatPatterns );
    }
    
    public static Date parseDate ( String text , TimeZone zone ) {
        return parseDate( text , zone , new String [ ] { "yyyy-MM-dd HH:mm:ss" , "yyyy-MM-dd" } );
    }
    
    public static Date parseDate ( String text , TimeZone zone , String [ ] dateFormatPatterns ) {
        if ( dateFormatPatterns != null ) {
            String [ ] arrayOfString;
            int j = (arrayOfString = dateFormatPatterns).length;
            for ( int i = 0 ; i < j ; ) {
                String pattern = arrayOfString [ i];
                try {
                    SimpleDateFormat parser = new SimpleDateFormat( pattern );
                    if ( zone != null )
                        parser.setTimeZone( zone );
                    return parser.parse( text );
                } catch ( Exception localException ) {
                    i++;
                }
                
            }
            
        }
        
        throw new RuntimeException( "Unparseable date: " + text );
    }
    
    /**
     * 将string(yyyy-MM-dd HH:mm:ss)转化为日期
     * 
     * @param dateString
     * @return
     * @throws ParseException
     **/
    public static Date parseDateTime ( String dateString ) throws ParseException {
        return DATETIME_FORMAT.parse( dateString );
    }
    
    public static Date parseTime ( String dateString ) throws ParseException {
        if ( dateString.trim( ).indexOf( " " ) > 0 ) {
            String [ ] d = dateString.trim( ).split( " " );
            if ( dateString.trim( ).indexOf( ":" ) != dateString.trim( ).lastIndexOf( ":" ) ) {
                return new java.sql.Timestamp( TIME_FORMAT.parse( d [ 1] ).getTime( ) );
            } else {
                return new java.sql.Timestamp( TIME_NOSECOND_FORMAT.parse( d [ 1] ).getTime( ) );
            }
        } else if ( dateString.indexOf( ":" ) > 0 ) {
            // 如果有两个:，则有时分秒,一个冒号只有时分
            if ( dateString.trim( ).indexOf( ":" ) != dateString.trim( ).lastIndexOf( ":" ) ) {
                return new java.sql.Time( TIME_FORMAT.parse( dateString ).getTime( ) );
            } else {
                return new java.sql.Time( TIME_NOSECOND_FORMAT.parse( dateString ).getTime( ) );
            }
        }
        return new java.sql.Date( DATETIME_FORMAT.parse( dateString ).getTime( ) );
    }
    
    /**
     * 按格式(yyyy-MM-dd HH:mm )输出 string到date
     * 
     * @param dateString
     * @return
     * @throws ParseException
     **/
    public static Date parseTimeNoSecond ( String dateString ) throws ParseException {
        return DATETIME_NOSECOND_FORMAT.parse( dateString );
    }
    
    /**
     * 字符串转换成日期格式
     * 
     * @param string 待转换的日期字符串
     * @param pattern 日期格式
     * @return {@link Date} 转换后的日期
     **/
    public static Date string2Date ( String string , String pattern ) {
        try {
            return new SimpleDateFormat( pattern ).parse( string );
        } catch ( ParseException e ) {
            throw new IllegalArgumentException( "无法将字符串[" + string + "]按格式[" + pattern + "]转换为日期" , e );
        }
    }
    
    /**
     * 
     * @param date是为则默认今天日期、可自行设置“2013-06-03”格式的日期
     * 
     * @return 返回1是星期日、2是星期一、3是星期二、4是星期三、5是星期四、6是星期五、7是星期六
     * 
     **/
    
    public static int getDayofweek ( String date ) {
        Calendar cal = Calendar.getInstance( );
        // cal.setTime(new Date(System.currentTimeMillis()));
        if ( date.equals( "" ) ) {
            cal.setTime( new Date( System.currentTimeMillis( ) ) );
        } else {
            cal.setTime( new Date( getDateByStr2( date ).getTime( ) ) );
        }
        return cal.get( Calendar.DAY_OF_WEEK );
    }
    
    public static Date getDateByStr2 ( String dd ) {
        
        SimpleDateFormat sd = new SimpleDateFormat( "yyyy-MM-dd" );
        Date date;
        try {
            date = sd.parse( dd );
        } catch ( ParseException e ) {
            date = null;
            e.printStackTrace( );
        }
        return date;
    }
    
}
